package com.caimiao.miao.page.home;

import android.content.Context;
import android.graphics.Bitmap;

import com.caimiao.miao.MiaoApplication;
import com.caimiao.miao.R;
import com.caimiao.miao.bean.TouchPoint;
import com.caimiao.miao.library.utils.DecimalUtil;
import com.caimiao.miao.library.utils.PixelUtil;
import com.caimiao.miao.library.utils.QrBmpUtil;

import java.security.PublicKey;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

import io.reactivex.Observable;
import io.reactivex.ObservableEmitter;
import io.reactivex.ObservableOnSubscribe;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;

public class HomeModel implements IHomeContact.Model {
    private int mCurIndex;

    private static final int TIME_INTERVAL = 5 * 1000;
    private final List<TouchPoint> mPointList = new ArrayList<>();
    private ObservableEmitter<Integer> mEmitter;
    private Disposable mDisposable;

    @Override
    public List<String> getAdverts(Context context) {
        List<String> dataList = new ArrayList<>();
        dataList.add("android.resource://" + context.getPackageName() + "/mipmap/" + R.mipmap.welcome);
        dataList.add("android.resource://" + context.getPackageName() + "/mipmap/" + R.mipmap.home);
        return dataList;
    }

    @Override
    public void setCurrentItem(int index) {
        mCurIndex = index;
    }

    @Override
    public int getNextItem() {
        return ++mCurIndex;
    }

    @Override
    public long getIntervalTime() {
        return 10 * 1000;
    }


    @Override
    public Bitmap getQrCode(Context context, int width, int height) {
        return QrBmpUtil.getQrBmp(context, width, height);
    }

    @Override
    public void addTouch(int x, int y) {
        if (mPointList.isEmpty()) startTiming();//第一次点击时，启动定时器
        mPointList.add(new TouchPoint(x, y, System.currentTimeMillis()));
        mEmitter.onNext(canJump());
    }

    @Override
    public Observable<Integer> getMasterEgg() {
        return Observable.create(new ObservableOnSubscribe<Integer>() {
            @Override
            public void subscribe(ObservableEmitter<Integer> emitter) throws Exception {
                mEmitter = emitter;
            }
        });
    }

    private void startTiming() {
        mDisposable = Observable.timer(TIME_INTERVAL, TimeUnit.MILLISECONDS)
                .subscribe(new Consumer<Long>() {
                    @Override
                    public void accept(Long aLong) throws Exception {
                        mPointList.clear();
                    }
                });
    }

    private int canJump() {
        if (mPointList.size() >= 3) {
            TouchPoint point1 = getNextPoint();
            TouchPoint point2 = getNextPoint();
            TouchPoint point3 = getNextPoint();
            long time = point3.getTime() - point1.getTime();
            if (time > TIME_INTERVAL) {
                return 0;///时间超出
            }

        }
        return 0;
    }

    private TouchPoint getNextPoint() {
        return mPointList.remove(0);
    }

}
